package com.tubang.logistics.controller.order;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.tubang.common.constant.Constants;
import com.tubang.common.util.*;
import com.tubang.component.entity.AddressEntity;
import com.tubang.component.service.IAddressService;
import com.tubang.component.util.RequestUtil;
import com.tubang.logistics.entity.*;
import com.tubang.logistics.service.*;
import com.tubang.logistics.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 平台运单表 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2017-12-31
 */
@RestController
@RequestMapping("/platform")
public class OrderPlatformController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private IShipperOrderService shipperOrderService;

    @Resource
    private IGoodsService goodsService;


    @Resource
    private IAddressService addressService;


    @Resource
    private ITransportService transportService;

    @Resource
    private IPlatformOrderService platformOrderService;

    @Resource
    private IPlatformOrderGoodsService platformOrderGoodsService;


    @Resource
    private ILogisticsOrderGoodsService logisticsOrderGoodsService;


    @Resource
    private ILogisticsOrderService logisticsOrderService;


    @Resource
    private ITruckerOrderService truckerOrderService;


    @Resource
    private ILogisticsOrderService logisticsOrderServices;

    /**
     * 平台订单调度列表
     *
     * @param
     * @return
     */
    @GetMapping(value = "platformDispatchList")
    public ResultData platformDispatchList(@RequestParam(required = false) String addrFrom,
                                           @RequestParam(required = false) String addrTo,
                                           @RequestParam(required = false) String goodsName,//货品名称
                                           @RequestParam(required = false, defaultValue = "1") Integer page,
                                           @RequestParam(required = false, defaultValue = "10") Integer rows) {
        try {
            //查询待接单的列表
            EntityWrapper<ShipperOrderEntity> entityWrapper = new EntityWrapper<ShipperOrderEntity>();
            entityWrapper.eq("status_code", Constants.ORDER_STATUS_CODE_RELEASE_GOODS);
            entityWrapper.orderBy("create_time",false);
            if (StringUtils.isNotEmpty(addrFrom)) {
                entityWrapper.eq("addr_from", addrFrom);
            }

            if (StringUtils.isNotEmpty(addrFrom)) {
                entityWrapper.like("addr_from", addrFrom);
            }


            if (StringUtils.isNotEmpty(addrTo)) {
                entityWrapper.like("addr_to", addrTo);
            }

            Page<ShipperOrderEntity> ShipperOrderPage = shipperOrderService.selectPage(new Page(page, rows), entityWrapper);
            List<ShipperOrderEntity> records = ShipperOrderPage.getRecords();
            List<OrderDispatchListVo> orderDispatchListVos = BeanMapper.mapList(records, OrderDispatchListVo.class);
            for (OrderDispatchListVo orderDispatchListVo : orderDispatchListVos) {
                String orderId = orderDispatchListVo.getId();
                //查询商品名称
                GoodsEntity goodsBasic = goodsService.selectBaseGoodsByOrderId(orderId, Constants.DEFAULT_VAULE_FU_ONE);
                String goodName = Constants.DEFAULT_STRING;

                //重量  名称
                if (goodsBasic != null) {
                    goodName = goodsBasic.getName();
                }
                //货品基本信息(未发布)
                GoodsBaseVo goodsBaseVo = BeanMapper.map(goodsBasic, GoodsBaseVo.class);
                orderDispatchListVo.setGoodsBaseVo(goodsBaseVo);
                orderDispatchListVo.setGoodsName(goodName);
                //货品详情 (未发步)
                BigDecimal weight = new BigDecimal(0);
                List<GoodsEntity> goodsEntities = goodsService.selectGoodssByOrderId(orderId, Constants.DEFAULT_VAULE_FU_ONE);
                for (GoodsEntity goodsEntity : goodsEntities) {
                    BigDecimal weight1 = goodsEntity.getWeight();
                    if (weight1 == null) continue;
                    weight = weight.add(weight1);
                }
                orderDispatchListVo.setWeight(String.valueOf(weight));
                List<GoodsInfoVo> goodsInfoVos = BeanMapper.mapList(goodsEntities, GoodsInfoVo.class);
                orderDispatchListVo.setGoodsInfoVoS(goodsInfoVos);
                TransportEntity transportEntity = transportService.selectByOrderId(orderId);
                //地址
                if (transportEntity != null) {
                    AddressEntity from = addressService.selectById(transportEntity.getFromUserAddressId());
                    AddressEntity to = addressService.selectById(transportEntity.getToUserAddressId());
                    orderDispatchListVo.setAddressFrom(from);
                    orderDispatchListVo.setAddresTo(to);
                }
            }
            int count = shipperOrderService.selectCount(entityWrapper);
            return ResultData.result(true).setData(orderDispatchListVos).setTotalCount(count);
        } catch (Exception e) {
            // logger.error("[OrderShipperController]<== method[supplyOrderList], 列表失败 :[{}]", e.getMessage());
            e.printStackTrace();
            return ResultData.result(false).setData(e);
        }
    }

//    /**
//     * 平台订单调度
//     *
//     * @param
//     * @return
//     */
//    @PostMapping(value = "platformDispatchOrder")
//    public ResultData platformDispatchOrder(HttpServletRequest request) {
//        try {
//            String receivePostJson = RequestUtil.receivePostJson(request);
//            OrderDispatchVo orderDispatchVo = GsonQuick.toObject(receivePostJson, OrderDispatchVo.class);
//
//            //区分物流公司   司机
//            Integer dispatchType = orderDispatchVo.getDispatchType();
//            Integer status = orderDispatchVo.getStatus();
//
//            //201-指派中
//            if (Constants.USER_TYPE_INSIDE_DRIVER.equals(String.valueOf(dispatchType))) {
//                //司机指派单 直接生成运单
//                truckerOrderService.creatTruckerOrder(orderDispatchVo);
//            } else if (Constants.USER_TYPE_OUTSIDE_COMPANY.equals(String.valueOf(dispatchType))) {
//                //公司指派单
//                logisticsOrderService.creatLogisticsOrder(orderDispatchVo);
//                List<String> orderIds = orderDispatchVo.getOrderIds();
//                for (String orderId : orderIds) {
//                    ShipperOrderEntity shipperOrderEntity = shipperOrderService.selectById(orderId);
//                    if (shipperOrderEntity == null) continue;
//                    shipperOrderEntity.setStatusCode(Constants.ORDER_WAITING_PICK_UP_GOODS);
//                    shipperOrderEntity.setStatusCodeDesc("待提货");
//                    shipperOrderService.insertOrUpdate(shipperOrderEntity);
//                }
//
//            } else {
//                //平台生成子订单  待抢单  待竞价
//                platformOrderService.creatPlatformOrder(orderDispatchVo);
//            }
//
//            return ResultData.result(true);
//        } catch (Exception e) {
//            logger.error("[OrderPlatformController]<== method[platformDispatchOrder], platformDispatchOrder :[{}]", e.getMessage());
//            e.printStackTrace();
//            return ResultData.result(false).setData(e);
//        }
//    }


    /**
     * 平台订单调度
     *
     * @param
     * @return
     */
    @PostMapping(value = "platformDispatchOrder")
    public ResultData platformDispatchOrder(HttpServletRequest request) {
        try {
            String receivePostJson = RequestUtil.receivePostJson(request);
            OrderDispatchVo orderDispatchVo = GsonQuick.toObject(receivePostJson, OrderDispatchVo.class);

//            //区分物流公司   司机
//            Integer dispatchType = orderDispatchVo.getDispatchType();
//
//            //201-指派中
//            if (Constants.USER_TYPE_INSIDE_DRIVER.equals(String.valueOf(dispatchType))) {
//                //司机指派单 直接生成运单
//                truckerOrderService.creatTruckerOrder(orderDispatchVo);
//            } else if (Constants.USER_TYPE_OUTSIDE_COMPANY.equals(String.valueOf(dispatchType))) {
//                //公司指派单
//                logisticsOrderService.creatLogisticsOrder(orderDispatchVo);
//                List<String> orderIds = orderDispatchVo.getOrderIds();
//                for (String orderId : orderIds) {
//                    ShipperOrderEntity shipperOrderEntity = shipperOrderService.selectById(orderId);
//                    if (shipperOrderEntity == null) continue;
//                    shipperOrderEntity.setStatusCode(Constants.ORDER_WAITING_PICK_UP_GOODS);
//                    shipperOrderEntity.setStatusCodeDesc("待提货");
//                    shipperOrderService.insertOrUpdate(shipperOrderEntity);
//                }
//
//            }

            //平台生成子订单  待抢单  待竞价  待接单(指派)
            platformOrderService.creatPlatformOrder(orderDispatchVo);

            return ResultData.result(true);
        } catch (Exception e) {
            logger.error("[OrderPlatformController]<== method[platformDispatchOrder], platformDispatchOrder :[{}]", e.getMessage());
            e.printStackTrace();
            return ResultData.result(false).setData(e);
        }
    }


    /**
     * 平台-(物流公司 司机)
     * <p>
     * 1,平台订单精选后 选择公司  司机   2, 公司, 司机 抢单
     *
     * @param
     * @return
     */
    @PostMapping(value = "platformConfirmOrder")
    public ResultData platformConfirmOrder(HttpServletRequest request) {
        try {
            String receivePostJson = RequestUtil.receivePostJson(request);
            OrderBiddingVo platformOrderBiddingVo = GsonQuick.toObject(receivePostJson, OrderBiddingVo.class);

            Date now = new Date();
            //更新主单状态
            String orderType = platformOrderBiddingVo.getOrderType();
            PlatformOrderEntity platformOrderEntity = platformOrderService.selectById(platformOrderBiddingVo.getOrderId());
            if (platformOrderEntity != null) {
                platformOrderEntity.setStatusCode(Constants.ORDER_WAITING_PICK_UP_GOODS);
                platformOrderEntity.setStatusCodeDesc("待提货");
                platformOrderEntity.setCarrierId(platformOrderBiddingVo.getCarrierId());
                platformOrderEntity.setCarrier(platformOrderBiddingVo.getCarrierName());
                platformOrderEntity.setCarrierMobile(platformOrderBiddingVo.getCarrierMobile());
                platformOrderEntity.setRemarks(platformOrderBiddingVo.getRemarks());
                Double carriage = platformOrderEntity.getCarriage();
                if (carriage != null) {
                    platformOrderEntity.setCarriage(carriage);
                }
                platformOrderEntity.setUpdateTime(now);
                platformOrderService.insertOrUpdate(platformOrderEntity);

                platformOrderBiddingVo.setUserId(platformOrderEntity.getUserId());
                //司机 抢单 接单 竞价最终选择
                if (Constants.USER_TYPE_INSIDE_DRIVER.equals(String.valueOf(orderType))) {
                    //司机指派单 直接生成运单
                    platformOrderBiddingVo.setGoodsWeight(platformOrderEntity.getGoodsWeight());
                    platformOrderBiddingVo.setGoodsName(platformOrderEntity.getGoodsName());
                    platformOrderBiddingVo.setCarriage(platformOrderEntity.getCarriage());
                    platformOrderBiddingVo.setAmount(platformOrderEntity.getAmountDeal());
                    truckerOrderService.creatTruckerOrderBeforePlatformOrder(platformOrderBiddingVo);
                    return ResultData.result(true);
                }

                //公司 抢单 接单 竞价最终选择
                if (Constants.USER_TYPE_OUTSIDE_COMPANY.equals(orderType)) {
                    platformOrderBiddingVo.setStatus(Constants.ORDER_STATUS_CODE_RELEASE_GOODS);
                    platformOrderBiddingVo.setStatusCodeDesc("待调度");
                    platformOrderBiddingVo.setGoodsWeight(platformOrderEntity.getGoodsWeight());
                    platformOrderBiddingVo.setGoodsName(platformOrderEntity.getGoodsName());
                    platformOrderBiddingVo.setCarriage(platformOrderEntity.getCarriage());
                    platformOrderBiddingVo.setAmount(platformOrderEntity.getAmountDeal());
                    logisticsOrderService.creatLogisticsOrder(platformOrderBiddingVo);
                }
            }

            return ResultData.result(true);
        } catch (Exception e) {
            logger.error("[OrderPlatformController]<== method[platformConfirmOrder], 更新状态失败 :[{}]", e.getMessage());
            e.printStackTrace();
            return ResultData.result(false).setData(e);
        }
    }


    /**
     * 更新平台订单状态  ( 平台 续选 )
     *
     * @param
     * @return
     */
    @PostMapping(value = "updateplatformOrderStatus")
    public ResultData updateplatformOrderStatus(HttpServletRequest request) {
        try {
            String receivePostJson = RequestUtil.receivePostJson(request);
            ChangeStatusVo changeStatusVo = GsonQuick.toObject(receivePostJson, ChangeStatusVo.class);
            //更新状态
            platformOrderService.updateplatformOrderStatus(changeStatusVo);
            return ResultData.result(true);
        } catch (Exception e) {
            logger.error("[OrderShipperController]<== method[updateSupplyOrderStatus], 更新状态失败 :[{}]", e.getMessage());
            e.printStackTrace();
            return ResultData.result(false).setData(e);
        }
    }


    /**
     * 平台订单改价
     *
     * @param
     * @return
     */
    @PostMapping(value = "platformOrderChangeCarriage")
    public ResultData platformOrderChangeCarriage(HttpServletRequest request) {
        try {
            String receivePostJson = RequestUtil.receivePostJson(request);
            PlatformOrderChangeCarriageVo platformOrderChangeCarriageVo = GsonQuick.toObject(receivePostJson, PlatformOrderChangeCarriageVo.class);
            Date now = new Date();
            PlatformOrderEntity platformOrderEntity = platformOrderService.selectById(platformOrderChangeCarriageVo.getOrderId());
            if (platformOrderEntity == null) ResultData.result(true);
            platformOrderEntity.setCarriageDeal(platformOrderChangeCarriageVo.getCarriage());
            Double carriage = platformOrderEntity.getCarriage();
            if(carriage<=0){
                carriage=1d;
            }
            double a = platformOrderEntity.getAmount() / carriage;
            platformOrderEntity.setAmountDeal(platformOrderChangeCarriageVo.getCarriage() * a);
            platformOrderEntity.setUpdateTime(now);
            platformOrderService.insertOrUpdate(platformOrderEntity);
            return ResultData.result(true);
        } catch (Exception e) {
            logger.error("[OrderPlatformController]<== method[platformOrderChangeCarriage], 更新状态失败 :[{}]", e.getMessage());
            e.printStackTrace();
            return ResultData.result(false).setData(e);
        }
    }


    /**
     * 平台订单管理详情
     *
     * @param
     * @return
     */
    @GetMapping(value = "platformOrderInfo")
    public ResultData platformOrderInfo(@RequestParam(required = false) String orderId) {
        try {

            OrderInfoVo orderbInfoVo = new OrderInfoVo();
            ShipperOrderEntity order = shipperOrderService.selectById(orderId);
            BeanMapper.copy(order, orderbInfoVo);
            TransportEntity transportEntity = transportService.selectByOrderId(orderId);
            //地址
            if (transportEntity != null) {
                AddressEntity from = addressService.selectById(transportEntity.getFromUserAddressId());
                AddressEntity to = addressService.selectById(transportEntity.getToUserAddressId());
                orderbInfoVo.setAddressFrom(from);
                orderbInfoVo.setAddresTo(to);
                orderbInfoVo.setTakePlanDate(transportEntity.getTakePlanDate());
                orderbInfoVo.setUnloadPlanDate(transportEntity.getUnloadPlanDate());
                orderbInfoVo.setUnloadPlanDateStr(DateFormatUtils.formatDate(transportEntity.getUnloadPlanDate(), "yyyy-MM-dd HH:mm:ss"));
                orderbInfoVo.setTakePlanDateStr(DateFormatUtils.formatDate(transportEntity.getTakePlanDate(), "yyyy-MM-dd HH:mm:ss"));
            }
            BeanMapper.copy(transportEntity, orderbInfoVo);
            //补充货物明细
            GoodsEntity goodsBasic = goodsService.selectBaseGoodsByOrderId(orderId);
            GoodsBaseVo goodsBaseVo = BeanMapper.map(goodsBasic, GoodsBaseVo.class);
            orderbInfoVo.setGoodsBaseVo(goodsBaseVo);
            List<GoodsEntity> goodss = goodsService.selectGoodssByOrderId(orderId);
            List<GoodsInfoVo> goodsInfoVos = BeanMapper.mapList(goodss, GoodsInfoVo.class);
            orderbInfoVo.setGoodsInfoVoS(goodsInfoVos);


            //查询商品在平台的运单数据
            List<String> numbs = new ArrayList<String>();
            List<OrderbillSimpleVo> orderbillSimpleVos = orderbInfoVo.getOrderbillSimpleVos();
            for (GoodsEntity goods : goodss) {
                PlatformOrderGoodsEntity platformOrderGoodsEntity = platformOrderGoodsService.selectOrderByGoodsId(goods.getId());
                if (platformOrderGoodsEntity == null) continue;
                PlatformOrderEntity platformOrderEntity = platformOrderService.selectById(platformOrderGoodsEntity.getPlatformOrderId());
                if (platformOrderEntity == null) continue;
                if (numbs.contains(platformOrderEntity.getNum())) continue;
                OrderbillSimpleVo orderbillSimpleVo = BeanMapper.map(platformOrderEntity, OrderbillSimpleVo.class);
                orderbillSimpleVos.add(orderbillSimpleVo);
                numbs.add(orderbillSimpleVo.getNum());
            }

            return ResultData.result(true).setData(orderbInfoVo);
        } catch (Exception e) {
            logger.error("[OrderPlatformController]<== method[updateSupplyOrderStatus], 更新状态失败 :[{}]", e.getMessage());
            e.printStackTrace();
            return ResultData.result(false).setData(e);
        }
    }


    /**
     * 平台子订单列表
     *
     * @param
     * @return
     */
    @GetMapping(value = "platformOrderBillList")
    public ResultData platformOrderBillList(@RequestParam(required = false) String addrFrom,
                                            @RequestParam(required = false) String addrTo,
                                            @RequestParam(required = false) String num,
                                            @RequestParam(required = false) String statusCode,//逗号隔开的  需要处理
                                            @RequestParam(required = false) String companyId,
                                            @RequestParam(required = false) String userId,
                                            @RequestParam(required = false, defaultValue = "0") String listType,//0 物流公司-订单管理列表  1,平台子订单列表
                                            @RequestParam(required = false, defaultValue = "1") Integer page,
                                            @RequestParam(required = false, defaultValue = "10") Integer rows) {
        try {

            EntityWrapper<PlatformOrderEntity> entityWrapper = new EntityWrapper<PlatformOrderEntity>();
            if (StringUtils.isNotEmpty(num)) {
                entityWrapper.eq("num", num);
            }
            if (StringUtils.isNotEmpty(statusCode)) {
                String[] split = statusCode.split(",");
                entityWrapper.in("status_code", split);
            }

            if (StringUtils.isNotEmpty(companyId)) {
                entityWrapper.eq("carrier_id", companyId);
            }

            if (StringUtils.isNotEmpty(addrFrom)) {
                entityWrapper.like("addr_from", addrFrom);
            }


            if (StringUtils.isNotEmpty(addrTo)) {
                entityWrapper.like("addr_to", addrTo);
            }


            Page<PlatformOrderEntity> platformOrderPage = platformOrderService.selectPage(new Page<PlatformOrderEntity>(page, rows), entityWrapper);
            List<PlatformOrderEntity> records = platformOrderPage.getRecords();
            List<OrderListVo> platformOrderListVos =new ArrayList<OrderListVo>();

            for (PlatformOrderEntity platformOrderEntity : records) {
                OrderListVo orderListVo = BeanMapper.map(platformOrderEntity, OrderListVo.class);
                String orderId = orderListVo.getId();
                List<PlatformOrderGoodsEntity> platformOrderGoodsEntities = platformOrderGoodsService.selectOrderByOrderId(orderId);
                //所有货品
                GoodsEntity goodsBasic = new GoodsEntity();
                for (PlatformOrderGoodsEntity platformOrderGoodsEntity : platformOrderGoodsEntities) {
                    goodsBasic = goodsService.selectBaseGoodsByGoodsId(platformOrderGoodsEntity.getGoodsId());
                }

                TransportEntity transportEntity = transportService.selectByOrderId(goodsBasic.getShipperOrderId());
                //地址
                if (transportEntity != null) {
                    AddressEntity from = addressService.selectById(transportEntity.getFromUserAddressId());
                    AddressEntity to = addressService.selectById(transportEntity.getToUserAddressId());
                    orderListVo.setAddressFrom(from);
                    orderListVo.setAddresTo(to);
                }
                //查询商品名称-
                orderListVo.setGoodsName(platformOrderEntity.getGoodsName());
                orderListVo.setWeight(String.valueOf(platformOrderEntity.getGoodsWeight()));
                orderListVo.setOrderType(Constants.USER_TYPE_OUTSIDE_PLATFORM);
                orderListVo.setGoodsWeight(platformOrderEntity.getGoodsWeight());
                platformOrderListVos.add(orderListVo);
            }
            int count = platformOrderService.selectCount(entityWrapper);


//            EntityWrapper<LogisticsOrderEntity> logisticsWrapper = new EntityWrapper<>();
//            //物流公司的订单管理
//
//            if (StringUtils.isNotEmpty(statusCode)) {
//                String[] split = statusCode.split(",");
//                logisticsWrapper.in("status_code", split);
//            }
//
//            if (StringUtils.isNotEmpty(companyId)) {
//                logisticsWrapper.in("carrier_id", companyId);
//            }
//            if (StringUtils.isNotEmpty(num)) {
//                logisticsWrapper.eq("num", num);
//            }
//
//            if (StringUtils.isNotEmpty(addrFrom)) {
//                logisticsWrapper.like("addr_from", addrFrom);
//            }
//
//
//            if (StringUtils.isNotEmpty(addrTo)) {
//                logisticsWrapper.like("addr_to", addrTo);
//            }
//
//            if (StringUtils.isNotEmpty(userId)) {
//                logisticsWrapper.like("user_id", userId);
//            }
//
//            int logisticsCount = logisticsOrderServices.selectCount(logisticsWrapper);
//            Page<LogisticsOrderEntity> logisticsOrderpPage = logisticsOrderServices.selectPage(new Page(page, rows), logisticsWrapper);
//            ArrayList<OrderListVo> logisticsOrderListVos = Lists.newArrayList();
//            List<LogisticsOrderEntity> list = logisticsOrderpPage.getRecords();
//            for (LogisticsOrderEntity lo : list) {
//                try {
//                    OrderListVo vo = BeanMapper.map(lo, OrderListVo.class);
//                    GoodsEntity goodsEntity = goodsService.selectBaseGoodsByLogisticsOrderId(lo.getId());
//                    TransportEntity transportEntity = transportService.selectById(goodsEntity.getTransportId());
//                    vo.setGoodsName(goodsEntity.getName());
//                    vo.setWeight(String.valueOf(goodsEntity.getWeight()));
//                    AddressEntity fAddressEntity = addressService.selectById(transportEntity.getFromUserAddressId());
//                    AddressEntity tAddressEntity = addressService.selectById(transportEntity.getToUserAddressId());
//                    vo.setAddressFrom(fAddressEntity);
//                    vo.setAddresTo(tAddressEntity);
//                    vo.setOrderType(Constants.USER_TYPE_OUTSIDE_COMPANY);
//                    logisticsOrderListVos.add(vo);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    logger.error("数据加工失败，物流公司运单编号为：{}==,,失败原因为{}", lo.getId(), e.getMessage());
//                    return ResultData.result(false).setData(e);
//                }
//            }
//           /* if(Constants.DEFAULT_VAULE_ONE.equals(listType)){
//                return ResultData.result(true).setData(logisticsOrderListVos).setTotalCount(logisticsCount);
//            }*/
//
//            ArrayList<OrderListVo> orderListVos = new ArrayList<OrderListVo>();
//            orderListVos.addAll(logisticsOrderListVos);
//            orderListVos.addAll(platformOrderListVos);

            return ResultData.result(true).setData(platformOrderListVos).setTotalCount(count);
        } catch (Exception e) {
            logger.error("[OrderPlatformController]<== method[platformOrderBillList], 更新状态失败 :[{}]", e.getMessage());
            e.printStackTrace();
            return ResultData.result(false).setData(e);
        }
    }


    /**
     * 平台运单详情
     *
     * @param
     * @return
     */
    @GetMapping(value = "platformOrderBillInfo")
    public ResultData platformOrderBillInfo(@RequestParam(required = false) String orderId,
                                            @RequestParam(required = false) String orderType) {
        try {

            OrderbillInfoVo orderbillInfoVo = new OrderbillInfoVo();

            if (Constants.USER_TYPE_OUTSIDE_COMPANY.equals(orderType)) {
                //公司
                LogisticsOrderEntity logisticsOrderEntity = logisticsOrderServices.selectById(orderId);
                orderbillInfoVo = BeanMapper.map(logisticsOrderEntity, OrderbillInfoVo.class);

                List<GoodsEntity> goodsEntities = goodsService.selectGoodssByLogisticsOrderId(orderId,null);
                List<GoodsInfoVo> goodsInfoVos = BeanMapper.mapList(goodsEntities, GoodsInfoVo.class);

                GoodsEntity baseGoods = goodsService.selectBaseGoodsByLogisticsOrderId(orderId);
                //货品列表
                GoodsBaseVo goodsBaseVo = new GoodsBaseVo();
                if (baseGoods != null) {
                    BeanMapper.copy(baseGoods, goodsBaseVo);
                    Double goodsWeight = logisticsOrderEntity.getGoodsWeight();
                    if (goodsWeight != null) {
                        goodsBaseVo.setWeight(new BigDecimal(goodsWeight));
                    }
                    String goodsName = logisticsOrderEntity.getGoodsName();
                    if (goodsName != null) {
                        goodsBaseVo.setName(goodsName);
                    }
                    TransportEntity transportEtity = transportService.selectById(baseGoods.getTransportId());
                    AddressEntity fAddressEntity = addressService.selectById(transportEtity.getFromUserAddressId());
                    AddressEntity tAddressEntity = addressService.selectById(transportEtity.getToUserAddressId());
                    orderbillInfoVo.setNote(transportEtity.getNote());
                    orderbillInfoVo.setAddressFrom(fAddressEntity);
                    orderbillInfoVo.setAddresTo(tAddressEntity);
                    BeanMapper.copy(transportEtity, orderbillInfoVo);
                    orderbillInfoVo.setUnloadPlanDateStr(DateFormatUtils.formatDate(transportEtity.getUnloadPlanDate(), "yyyy-MM-dd HH:mm:ss"));
                    //  orderbillInfoVo.setUnloadRealDateStr(DateFormatUtils.formatDate(transportEtity.getUnloadRealDate(), "yyyy-MM-dd HH:mm:ss"));
                    orderbillInfoVo.setTakePlanDateStr(DateFormatUtils.formatDate(transportEtity.getTakePlanDate(), "yyyy-MM-dd HH:mm:ss"));
                    //  orderbillInfoVo.setTakeRealDateStr(DateFormatUtils.formatDate(transportEtity.getTakeRealDate(), "yyyy-MM-dd HH:mm:ss"));

                }
                orderbillInfoVo.setGoodsBaseVo(goodsBaseVo);
                orderbillInfoVo.setGoodsInfoVoS(goodsInfoVos);
            }
            if (Constants.USER_TYPE_OUTSIDE_PLATFORM.equals(orderType)) {
                //平台
                PlatformOrderEntity platformOrderEntity = platformOrderService.selectById(orderId);
                BeanMapper.copy(platformOrderEntity, orderbillInfoVo);

                List<PlatformOrderGoodsEntity> platformOrderGoodsEntities = platformOrderGoodsService.selectOrderByOrderId(orderId);
                String shipperId = "";
                GoodsEntity goodsBasic = new GoodsEntity();
                List<GoodsInfoVo> goodsInfoVos = new ArrayList<GoodsInfoVo>();
                for (PlatformOrderGoodsEntity platformOrderGoodsEntity : platformOrderGoodsEntities) {
                    goodsBasic = goodsService.selectBaseGoodsByGoodsId(platformOrderGoodsEntity.getGoodsId());
                    GoodsBaseVo goodsBaseVo = BeanMapper.map(goodsBasic, GoodsBaseVo.class);
                    //重量  名称
                    if (goodsBasic != null) {
                        shipperId = goodsBasic.getShipperOrderId();
                    }
                    GoodsEntity goodsEntity = goodsService.selectById(platformOrderGoodsEntity.getGoodsId());
                    if (goodsEntity == null || goodsEntity.getParentId().equals("0")) continue;
                    GoodsInfoVo goodsInfoVo = BeanMapper.map(goodsEntity, GoodsInfoVo.class);
                    Double goodsWeight = platformOrderEntity.getGoodsWeight();
                    if (goodsWeight != null) {
                        goodsBaseVo.setWeight(new BigDecimal(goodsWeight));
                    }
                    String goodsName = platformOrderEntity.getGoodsName();
                    if (goodsName != null) {
                        goodsBaseVo.setName(goodsName);
                    }
                    orderbillInfoVo.setGoodsBaseVo(goodsBaseVo);
                    goodsInfoVos.add(goodsInfoVo);
                }
                orderbillInfoVo.setGoodsInfoVoS(goodsInfoVos);
                TransportEntity transportEntity = transportService.selectByOrderId(shipperId);
                //地址
                if (transportEntity != null) {
                    AddressEntity from = addressService.selectById(transportEntity.getFromUserAddressId());
                    AddressEntity to = addressService.selectById(transportEntity.getToUserAddressId());
                    orderbillInfoVo.setAddressFrom(from);
                    orderbillInfoVo.setAddresTo(to);
                    orderbillInfoVo.setUnloadPlanDateStr(DateFormatUtils.formatDate(transportEntity.getUnloadPlanDate(), "yyyy-MM-dd HH:mm:ss"));
                    // orderbillInfoVo.setUnloadRealDateStr(DateFormatUtils.formatDate(transportEntity.getUnloadRealDate(),"yyyy-MM-dd HH:mm:ss"));
                    orderbillInfoVo.setTakePlanDateStr(DateFormatUtils.formatDate(transportEntity.getTakePlanDate(), "yyyy-MM-dd HH:mm:ss"));
                    //orderbillInfoVo.setTakeRealDateStr(DateFormatUtils.formatDate(transportEntity.getTakeRealDate(),"yyyy-MM-dd HH:mm:ss"));
                }
                BeanMapper.copy(transportEntity, orderbillInfoVo);
            }
            return ResultData.result(true).setData(orderbillInfoVo);
        } catch (Exception e) {
            logger.error("[OrderPlatformController]<== method[supplyOrderBillInfo], 查询详情 :[{}]", e.getMessage());
            e.printStackTrace();
            return ResultData.result(false).setData(e);
        }
    }

    /**
     * 物流公司订单详情
     *
     * @param
     * @return
     */
    @GetMapping(value = "logisticsOrderInfo")
    public ResultData logisticsOrderInfo(@RequestParam(required = false) String orderId) {
        try {
            OrderInfoVo orderbInfoVo = new OrderInfoVo();
            PlatformOrderEntity platformOrderEntity = platformOrderService.selectById(orderId);
            BeanMapper.copy(platformOrderEntity, orderbInfoVo);
            List<PlatformOrderGoodsEntity> platformOrderGoodsEntities = platformOrderGoodsService.selectOrderByOrderId(orderId);
            String shipperId = "";
            //补充货物明细
            GoodsEntity goodsBasic = new GoodsEntity();
            GoodsBaseVo goodsBaseVo = new GoodsBaseVo();
            for (PlatformOrderGoodsEntity platformOrderGoodsEntity : platformOrderGoodsEntities) {
                goodsBasic = goodsService.selectBaseGoodsByGoodsId(platformOrderGoodsEntity.getGoodsId());
                goodsBaseVo = BeanMapper.map(goodsBasic, GoodsBaseVo.class);
                //重量  名称
                if (goodsBasic != null) {
                    shipperId = goodsBasic.getShipperOrderId();
                }
            }
            List<GoodsEntity> goodsEntities = goodsService.selectGoodssByOrderId(shipperId);
            List<GoodsInfoVo> goodsInfoVos = BeanMapper.mapList(goodsEntities, GoodsInfoVo.class);
            orderbInfoVo.setGoodsBaseVo(goodsBaseVo);
            orderbInfoVo.setGoodsInfoVoS(goodsInfoVos);
            //运输详情
            TransportEntity transportEntity = transportService.selectByOrderId(shipperId);
            //地址
            if (transportEntity != null) {
                AddressEntity from = addressService.selectById(transportEntity.getFromUserAddressId());
                AddressEntity to = addressService.selectById(transportEntity.getToUserAddressId());
                orderbInfoVo.setAddressFrom(from);
                orderbInfoVo.setAddresTo(to);
                orderbInfoVo.setUnloadPlanDateStr(DateFormatUtils.formatDate(transportEntity.getUnloadPlanDate(), "yyyy-MM-dd HH:mm:ss"));
                orderbInfoVo.setTakePlanDateStr(DateFormatUtils.formatDate(transportEntity.getTakePlanDate(), "yyyy-MM-dd HH:mm:ss"));
            }
            BeanMapper.copy(transportEntity, orderbInfoVo);
            //查询商品在物流公司的运单数据

            //查询商品在平台的运单数据
            List<OrderbillSimpleVo> orderbillSimpleVos = orderbInfoVo.getOrderbillSimpleVos();
            List<String> numbs = new ArrayList<String>();
            for (GoodsEntity goods : goodsEntities) {
                LogisticsOrderGoodsEntity logisticsOrderGoodsEntity = logisticsOrderGoodsService.selectOrderByGoodsId(goods.getId(), null);
                if (logisticsOrderGoodsEntity == null) continue;
                LogisticsOrderEntity logisticsOrderEntity = logisticsOrderService.selectById(logisticsOrderGoodsEntity.getLogisticsOrderId());
                if (logisticsOrderEntity == null) continue;
                //同一订单商品
                if (numbs.contains(logisticsOrderEntity.getId())) continue;
                OrderbillSimpleVo orderbillSimpleVo = BeanMapper.map(logisticsOrderEntity, OrderbillSimpleVo.class);
                orderbillSimpleVos.add(orderbillSimpleVo);
            }
            return ResultData.result(true).setData(orderbInfoVo);
        } catch (Exception e) {
            logger.error("[OrderShipperController]<== method[supplyOrderBillInfo], 查询详情 :[{}]", e.getMessage());
            e.printStackTrace();
            return ResultData.result(false).setData(e);
        }
    }




}
